/**
 * Created by H2 on 2016/1/12.
 */

$(document).ready(function () {

    //定义url变量
    var userInfoURL = "/api/client/agent/agentlist";

    //定义分页变量
    var count = 5;
    var nowPage = 1;
    var jsonData = '';

    //定义map
    var userMap = {
        "#userTemp" : "#agMap-paging"
    };


    /**
     * 调用分页函数
     *
     * */
    function paging($obj,typeOrder,showType,nowPage,count,callback) {
        doRender(
            userInfoURL,
            {
                "agentTypeOrder": typeOrder,
                "start" : (nowPage - 1) * count,
                "length": count,
                "order" : showType,
                "t": now()
            },
            userMap,
            function (XMLHttpRequest) {
                //获取total

                var total = eval('(' + XMLHttpRequest.responseText + ')').total;
                if (total > 0) {

                    //在目标节点后append分页组件
                    $obj.append(genPagination(total, count, nowPage));
                    initPagination($obj,typeOrder,showType,nowPage,count,callback);
                    loadAgent();
                }
            }
        );
    }

    //调用分页函数
    paging($("#agMap-paging"),0,3,nowPage,count,paging);

    //分页按钮逻辑实现
    function initPagination($obj,typeOrder,showType,nowPage,count,callback) {

        $('.jcb-page-first').click(function () {
            if (nowPage == 1) return;
            nowPage = 1;
            callback($obj,typeOrder,showType,nowPage,count,callback);
        });

        $('.jcb-page-last').click(function () {
            if (nowPage == $(this).data('page')) return;
            nowPage = $(this).data('page');
            callback($obj,typeOrder,showType,nowPage,count,callback);
        });

        $('.jcb-page-next').click(function () {
            if (nowPage >= $(this).data('page')) return;
            nowPage++;
            callback($obj,typeOrder,showType,nowPage,count,callback);
        });

        $('.jcb-page-pre').click(function () {
            if (nowPage <= $(this).data('page')) return;
            nowPage--;
            callback($obj,typeOrder,showType,nowPage,count,callback);
        });

        $('.jcb-page-this').click(function () {
            if (nowPage == $(this).data('page')) return;
            nowPage = $(this).data('page');
            callback($obj,typeOrder,showType,nowPage,count,callback);
        });
    }

    /* JsRender 自定义标签 */
    $.views.tags({
       showStars: function(arNumber){
           var res = "";
           if(arNumber){
               for(var i=0; i<arNumber; i++){
                   res += '<span class="pList-detail-collect icon-collect-pitchon pull-center" style="line-height: 45px;"></span>';
               }

               for(var j=0; j<5-arNumber; j++){
                   res += '<span class="pList-detail-collect pull-center icon-collect-1" style="line-height: 45px;"></span>';
               }
               return res;
           }else{
               for(var i=0; i<5; i++){
                   res += '<span class="pList-detail-collect pull-center icon-collect-1" style="line-height: 45px;"></span>';
               }
               return res;
           }
       }
    });

    /* 按姓名排序 */
    $("#agName").click(function(){
        paging($("#agMap-paging"),0,0,nowPage,count,paging);
    });

    /* 按专业领域排序 */
    $("#agPro").click(function(){
        paging($("#agMap-paging"),0,1,nowPage,count,paging);
    });

    /* 按专业等级排序 */
    $("#agRank").click(function(){
        paging($("#agMap-paging"),0,3,nowPage,count,paging);
    });

    function loadAgent(){
        //经纪人回调函数
        $('.agMap-userMes').click(function () {
            location.assign("/detail/agentDetail/agentDetail?agentUserId=" + $(this).data("agentuserid"));
        })
    }

    /**
     * 异步加载百度地图部分
     **/
    (function loadMap() {
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = "http://api.map.baidu.com/api?v=2.0&ak=sgK6ypVfp11BPDC9p45i4h2F&callback=init";
        document.body.appendChild(script);
    })();

});

   /**
    *  百度地图回调函数
    **/
   function init() {
       var map = new BMap.Map("allmap");                            // 创建Map实例
       map.centerAndZoom(new BMap.Point(116.31361,39.980901),17);  // 创建中心点和初始缩放比例

       var markerArr = [
           {
               point: "116.31361,39.980901",
               icon: {w: 55, h: 55, l: 0, t: 0, x: 0, lb: 0},
               sex: 0
           },
           {
               point: "116.316758,39.978376",
               icon: {w: 55, h: 55, l: 0, t: 0, x: 0, lb: 0},
               sex: 1
           },
           {
               point: "116.307865,39.978321",
               icon: {w: 55, h: 55, l: 0, t: 0, x: 0, lb: 0},
               sex: 1
           },
           {
               point: "116.31719,39.983891",
               icon: {w: 55, h: 55, l: 0, t: 0, x: 0, lb: 0},
               sex: 0
           },
           {
               point: "116.305601,39.982343",
               icon: {w: 55, h: 55, l: 0, t: 0, x: 0, lb: 0},
               sex: 1
           }
       ];

       var aaa = '<img class="agMap_UserImg" src="/resources/app/images/head__.png">'+'<i class="agMap_squ"></i>'+
           '<span class="agMap_userName">张思明</span>' + '<p class="agMap_userRank">★★☆☆☆</p>';

       var labelArr = [];
       for(var i=0; i<markerArr.length; i++){
           var userJson = markerArr[i];

           var pointX = userJson.point.split(",")[0];  //图标x坐标
           var pointY = userJson.point.split(",")[1];  //图表y坐标
           var point = new BMap.Point(pointX,pointY);  //建立坐标点
           var userImageURL = userJson.sex == 0 ? "/resources/app/images/brokers_yellow.png" : "/resources/app/images/brokers_blue.png";

           var iconImage = createIcon(userJson.icon,userImageURL);    //绘制图标背景
           var marker = new BMap.Marker(point,{icon:iconImage});  //按照传入的 点和 图表背景 创建marker
           var label = new BMap.Label(aaa,{"offset":new BMap.Size(-90,-85)});  //创建每一个marker的label标签

           label.setStyle({
               borderColor:"#fafafa",
               cursor:"pointer",
               height:"70px",
               fontSize:"18px",
               borderRadius:"5px",
               paddingLeft:"220px"
           });

           marker.setLabel(label);  //将label绑定到marker上
           map.addOverlay(marker);  //将marker绑定到map上


           labelArr.push(marker);
           console.log("----------");
           console.log(labelArr);

           (function(){
               var index = i;
               marker.getLabel().hide();
               marker.addEventListener("click",function(){
                   for(var i=0; i<labelArr.length; i++){
                       labelArr[i].getLabel().hide();
                   }

                   labelArr[index].getLabel().show();
               });
           })(i);
       }

       //创建一个Icon
       function createIcon(json,imgUrl){
           var icon = new BMap.Icon(imgUrl, new BMap.Size(json.w,json.h),
               {
                   imageOffset: new BMap.Size(-json.l,-json.t)
               });
           return icon;
       }
   }

